package com.example.yypt.server;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import com.example.yypt.dao.dx_dxb000Mapper;
import com.example.yypt.model.HttpResult;
import com.example.yypt.model.dx_dxb000;
import com.example.yypt.model.xml.Response;
import com.example.yypt.utils.HttpClient;
import com.example.yypt.utils.XmlUtilsHelp;

import ch.qos.logback.core.joran.spi.XMLUtil;

@Service
public class DXServer {
	
	protected static Logger log = LoggerFactory.getLogger(DXServer.class);
	@Autowired
	private dx_dxb000Mapper dx_dxb000Mapper;

	@Autowired
	private HttpAPIService httpAPIService;

	@Value("${dx.url}")
	private String url;

	@Value("${dx.OperID}")
	private String OperID;

	@Value("${dx.OperPass}")
	private String OperPass;

	public void sendMsg() throws UnsupportedEncodingException {
		List<dx_dxb000> list = dx_dxb000Mapper.getdxbList();
		if (list.size()>0) {
			for (dx_dxb000 dx_dxb000 : list) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("OperID", OperID);
				map.put("OperPass", OperPass);
				map.put("DesMobile", dx_dxb000.getJshm00());
				map.put("Content",dx_dxb000.getDxnr00());
				map.put("Content_Code", 1);
				try {
					String context = httpAPIService.doGet(url, map);
					log.info("接收内容 " + context);
					Response resp= (Response) XmlUtilsHelp.xmlToBean(context);
					if ("03".equals(resp.getCode())) {
						dx_dxb000.setDxzt00("1");
						dx_dxb000Mapper.updateByPrimaryKey(dx_dxb000);
						log.info("成功发送: " + dx_dxb000.getJshm00());
					} else  {
						dx_dxb000.setDxzt00("2");
						dx_dxb000Mapper.updateByPrimaryKey(dx_dxb000);
						log.error("发送失败" + dx_dxb000.getJshm00() + "，原因"
								+context );
					}
				} catch (Exception e) {
					log.error(e.getMessage());
				}
			}
		}
		
	}
	/**
	 * 测试
	 * @param res
	 */
	public static void Test(String res) {
		Response resp= (Response) XmlUtilsHelp.xmlToBean(res);
		System.out.println(resp.toString());
	}
}
